﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.houseWork.Bll;
using CIT.WMS.Common;
using CIT.WMS.houseWork.Helper;
namespace CIT.WMS.houseWork
{
    public partial class FrmMaterialBack : Form
    {
        DataTable dt_QCResult = new DataTable();
        DataTable dt_Stock = new DataTable();
        //退料单据号
        string s_Doc = string.Empty;
        //料号
        string materialCode = string.Empty;
        int qty = 0;
        //物料SN
        string serialNumber = string.Empty;
        public FrmMaterialBack()
        {
            InitializeComponent();
        }

        private void txt_serialNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                DataTable dt_OutStock = Bllb_StockInfo_tbsi.IsOutStock(txt_serialNumber.Text.Trim());
                if (dt_OutStock.Rows.Count == 0)
                {
                    SetLabelMsg(false, "物料SN不属于退料状态", "sn");
                    return;
                }

                materialCode = ConvertUtils.ChangeNullToString(dt_OutStock.Rows[0]["MaterialCode"]);
                //dt_QCResult = Bllb_IQCDoc_tbid.QCResult(txt_serialNumber.Text.Trim());
                //if (dt_QCResult.Rows.Count == 0)
                //{
                //    SetLabelMsg(false, "未进行QC检验不能入库", "sn");
                //    return;
                //}
                txt_locationSN.Focus();
                string sqlcmd = string.Format(@"SELECT TOP 1 a.QTY  FROM [dbo].[T_Bllb_StorageDocDetail_tbsdd] AS a
LEFT JOIN dbo.T_Bllb_StorageDoc_tbsd AS  b ON a.S_Doc_NO=b.S_Doc_NO 
WHERE b.S_Doc_Type='6' AND a.Receive_Flag='N' AND a.SerialNumber='{0}' ORDER BY A.Create_Time DESC", txt_serialNumber.Text.Trim());
                DataTable dt_qty = Utils.NMS.QueryDataTable(Utils.uContext, sqlcmd, new CIT.WMS.MESWcf.CmdParameter[0]);
                if (dt_qty.Rows.Count > 0)
                {
                    qty = Convert.ToInt32(dt_qty.Rows[0]["QTY"]);
                }
                else
                {
                    SetLabelMsg(false, "参数异常，不能入库", "sn");
                    return;
                }
                //获取退料单据
                string strSql = string.Format(@"SELECT TOP 1 * FROM T_Bllb_StorageDoc_tbsd a
LEFT JOIN T_Bllb_StorageDocDetail_tbsdd  b ON a.S_Doc_NO=b.S_Doc_NO WHERE b.SerialNumber='{0}' AND a.S_Doc_Type='6' AND a.Close_Flag='N' ORDER BY a.Create_Time desc", txt_serialNumber.Text.Trim());
                DataTable dt_SDoc = Utils.NMS.QueryDataTable(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0]);
                s_Doc = dt_SDoc.Rows[0]["S_Doc_NO"].ToString();
                txt_locationSN.ReadOnly = false;

            }
        }



        private void txt_locationSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (txt_locationSN.ReadOnly == true)
                {
                    return;
                }
                if (txt_locationSN.Text.Trim() == string.Empty)
                {
                    //if (dt_QCResult.Rows[0]["RESULT"].ToString() == "2")
                    //{
                    //    SetLabelMsg(false, "拒收时库位不可为空", "location");
                    //    return;
                    //}
                    //if (dt_QCResult.Rows[0]["RESULT"].ToString() == "1")
                    //{
                    string strWhere = string.Format("  WHERE a.Storage_Name='原料仓'");
                    dt_Stock = Bllb_Storage_tbs.QueryLocation(strWhere);
                    string stockSN = dt_Stock.Rows[0]["Storage_SN"].ToString();
                    if (Bllb_Storage_tbs.UpdateStockInfo(txt_serialNumber.Text.Trim(), stockSN, qty, s_Doc))
                    {
                        serialNumber = txt_serialNumber.Text.Trim();
                        SetLabelMsg(true, "退料入库成功", "ok");
                        CIT.WMS.Common.PublicUtils.WriteLog(serialNumber, "退料入库", materialCode, qty);
                        return;
                    }
                    //}
                }
                else
                {
                    Input();
                }
            }
        }
        private void Input()
        {
            string strStockSN = string.Format("  WHERE c.Location_SN='{0}'", txt_locationSN.Text.Trim());
            //if (dt_QCResult.Rows[0]["RESULT"].ToString() == "2")
            //{
            //strStockSN += string.Format("and a.Storage_Type='2' ");
            strStockSN += string.Format("and a.Storage_Type='1' ");
            //}
            dt_Stock = Bllb_Storage_tbs.QueryLocation(strStockSN);
            if (dt_Stock.Rows.Count == 0)
            {
                SetLabelMsg(false, "库位不存在", "location");
                return;
            }
            string stockSN = SqlInput.ChangeNullToString(dt_Stock.Rows[0]["Storage_SN"]);
            string areaSN = SqlInput.ChangeNullToString( dt_Stock.Rows[0]["Area_SN"]);
            string locationSN = SqlInput.ChangeNullToString( dt_Stock.Rows[0]["Location_SN"]);
            if (Bllb_Storage_tbs.UpdateStock(txt_serialNumber.Text.Trim(), stockSN, areaSN, locationSN, qty, s_Doc))
            {
                serialNumber = txt_serialNumber.Text.Trim();
                SetLabelMsg(true, "退料入库成功", "ok");
                CIT.WMS.Common.PublicUtils.WriteLog(serialNumber, "退料入库", materialCode, qty);
                return;
            }
        }

        private void SetLabelMsg(bool showflag, string showMsg, string controlname)
        {
            if (showflag)
            {
                lab_msg.ForeColor = Color.DarkGreen;
            }
            else
            {
                lab_msg.ForeColor = Color.Red;
            }
            lab_msg.Text = showMsg;
            if (controlname.Contains("sn"))
            {
                txt_serialNumber.SelectAll();
                txt_serialNumber.Focus();
            }
            else if (controlname.Contains("location"))
            {
                txt_locationSN.SelectAll();
                txt_locationSN.Focus();
            }
            else if (controlname.Contains("ok"))
            {
                txt_serialNumber.Text = string.Empty;
                txt_locationSN.Text = string.Empty;
                txt_serialNumber.Focus();
            }
        }

        private void FrmMaterialBack_Load(object sender, EventArgs e)
        {

            txt_locationSN.ReadOnly = true;
        }
    }
}